DodoRunner.GameController = (function($, _, app){
var
    observable = {},

    gameover = false,

    init = function(){
        initEvents();
        initKeyEvents();
        initBackgrounds();
        initCollider();
        initObstacleGenerator();
        initGroundGenerator();
        initShip();
    },

    initGroundGenerator = function(){
        DodoRunner.DodoModel.on("move", function(event, x){
            //console.log(app.GroundModel.getGroundData().length , Math.floor(-app.Canvas.getCameraX()/48) + 32);
            if(app.GroundModel.getGroundData().length < Math.floor(-app.Canvas.getCameraX()/48) + 32) {
                DodoRunner.GroundModel.generate();
            }
        });
    },

    initObstacleGenerator = function(){
        app.ObstacleGenerator.init();
    },

    initCollider = function(){
        app.Collision.registerTracingObject(app.DodoModel);
        app.Collision.registerGround(app.GroundModel);
        app.Collision.on("CollisionEvent", function(event, data){
            if(!gameover){
                gameover = true;
                setTimeout(function(){
                    app.Canvas.togglePause();
                    setTimeout(function(){
                        var lastPoints = localStorage.last || 0;
                        var points =  -DodoRunner.Canvas.getCameraX();
                        localStorage.last = points;
                        $("body").empty();
                        $("body").append("<div id='leftpoints'>Last Game:" + lastPoints +"</div>");
                        $("body").append("<div id='rightpoints'>Now:" + points +"</div>");
                        $("body").append("<img src='res/images/end.png'></img>");
                        var $endbutton = $("<img style='position: fixed; left: 387px; top: 70px; cursor: pointer;' src='res/images/replay.png'></img>");
                        $("body").append($endbutton);
                        $endbutton.click(function(){
                            location.reload();
                        });
                    }, 250);
                }, 250);
            app.DodoModel.die();
            }
        });
        app.Collision.update();
    },

    initEvents = function(){
        DodoRunner.DodoModel.on("move", function(event, x){
            DodoRunner.Canvas.setCameraX(-x+100);
            $("#points").text("Points: " + -DodoRunner.Canvas.getCameraX());
        });
    },

    initKeyEvents = function(){
        $("body").keypress(function(event){
            $(observable).trigger("keyPress", event.charCode);
        });
    },

    initBackgrounds = function(){
        DodoRunner.Background("background", -0.5, -4);
        DodoRunner.Background("cloud_01", -1, -2, -100);
        DodoRunner.Background("cloud_02", -0.5, -1, -200);
        DodoRunner.Background("cloud_03", -2, 0, -100);
    },

    initShip = function(){
        var spriteSheet = new createjs.SpriteSheet({
            images: [DodoRunner.AssetManager.getImageByName("ship")],
            frames: {
                width: 512,
                height: 512
            },
            animations: {
                swim	:[ 0,  3]
            }
        });
        var sprite = new createjs.BitmapAnimation(spriteSheet);
        sprite.gotoAndStop("swim");
        sprite.x = 560;
        var x = sprite.x;
        var randomX = function(){
            x += (Math.random()-0.5)*3;
            sprite.x = DodoRunner.DodoModel.getX() + x;
            requestAnimationFrame(randomX);
        }
        var step = 0;
        var animate = function(){
            step++;
            if(step > 4){
                step = 0;
                sprite.advance();
            }
            requestAnimationFrame(animate);
        }
        animate();
        randomX();
        sprite.y = 230;
        sprite.z = -1;
        DodoRunner.Canvas.addChild(sprite);
    }

    return {
        init : init,
        on : function(event, callback){
            $(observable).on(event, callback);
        }
    }

}(jQuery, _, DodoRunner));